﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Views.Grid;
namespace QLTV.Forms.KeHoach
{
    public partial class frmKeHoachNganHan : BaseForm
    {

        public frmKeHoachNganHan()
        {
            InitializeComponent();
            Database.dsPermission dspermision1 = new Database.dsPermission();
            this.Datasource = dspermision1;
        }


        public int PlanID { get; set; }
        public bool isNew { get; set; }
        public bool isDone { get; set; }
        public bool isView { get; set; }

        private void checkPermission()
        {
            //quyen sua
            cmdAdd.Enabled = MyPermission.Sua;
            cmdChon.Enabled = MyPermission.Sua;
            cmdClear.Enabled = MyPermission.Sua;
            cmdXoa.Enabled = MyPermission.Sua;
            cmdDone.Enabled = MyPermission.Sua;
            cmdSave.Enabled = MyPermission.Sua;
            viewNganHan.OptionsBehavior.Editable = MyPermission.Sua;
            //quyen xuat
            cmdXuat.Enabled = MyPermission.Xuat;
        }

        private void frmKeHoachNganHan_Load(object sender, EventArgs e)
        {
            checkPermission();
            if (isView)
            {
                cmdSave.Enabled = false;
                cmdClose.Enabled = false;
                cmdXoa.Enabled = false;
            }
            if (isDone)
            {
                cmdSave.Enabled = false;
                cmdXoa.Enabled = false;
            }

            loadData();

        }

        private void dateThang_EditValueChanged(object sender, EventArgs e)
        {

        }
        private void loadData()
        {
            // TODO: This line of code loads data into the 'dsLongPlan.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsLongPlan.tblRank);
            // TODO: This line of code loads data into the 'dsShortPlan.tblCrew' table. You can move, or remove it, as needed.
            this.tadCrew.Fill(this.dsShortPlan.tblCrew);
            // TODO: This line of code loads data into the 'dsShortPlan.tblVesselGroup' table. You can move, or remove it, as needed.
            this.tblVesselGroupTableAdapter.Fill(this.dsShortPlan.tblVesselGroup);
            // TODO: This line of code loads data into the 'dsShortPlan.tblVessel' table. You can move, or remove it, as needed.
            this.tblVesselTableAdapter.Fill(this.dsShortPlan.tblVessel);

            if (isNew)
            {
                tblKeHoachBindingSource.AddNew();
                datePlanCreated.EditValue = DateTime.Now.Date;
                txtNguoiLap.EditValue = Program.EmployeeName;
                dateThang.EditValue = DateTime.Now.Date;
                isDone = false;
                dateDuKienThay.EditValue = DateTime.Now.Date.AddDays(7);
            }
            else
            {

                this.tblKeHoachTableAdapter.FillByID(this.dsShortPlan.tblKeHoach, PlanID);
                if (Program.language == "en")
                {
                    layoutLapKH.Text = "Modified Date";
                    layoutNguoiTao.Text = "Modified By";
                }
                else
                {
                    layoutLapKH.Text = "Ngày chỉnh sửa";
                    layoutNguoiTao.Text = "Người chỉnh sửa";
                }
                datePlanCreated.Properties.ReadOnly = true;
                datePlanCreated.DataBindings.RemoveAt(0);
                this.datePlanCreated.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.tblKeHoachBindingSource, "Ngày chỉnh sửa", true));
                datePlanCreated.EditValue = DateTime.Now.Date;



                txtNguoiLap.DataBindings.RemoveAt(0);
                txtNguoiLap.DataBindings.Add(new Binding("EditValue", tblKeHoachBindingSource, "NguoiChinhSua", true));
                txtNguoiLap.EditValue = Program.EmployeeName;

                tblShortPlanTableAdapter.FillBy(dsShortPlan.tblShortPlan, PlanID);
                cmdChon.Enabled = !(isDone);
                cmdClear.Enabled = !(isDone);
                cmdAdd.Enabled = !(isDone);
                cmdDone.Enabled = !(isDone);
                cmdChon.Enabled = false;
                cmdClear.Enabled = false;

            }
            loadVessel();
        }
        private void loadVessel()
        {
            if (!isNew)
            {
                int[] VesselList = (from q in dsShortPlan.tblShortPlan.AsEnumerable()
                                    where q.PlanID == PlanID
                                    select q.VesselID).Distinct().ToArray<int>();
                var query2 = from v in dsShortPlan.tblVessel.AsQueryable()
                             where VesselList.Contains(v.nVesselId)
                             select v;
                BindingSource source = new BindingSource();
                source.DataSource = query2;
                lookupVessel.Properties.DisplayMember = "cVesselName";
                lookupVessel.Properties.ValueMember = "nVesselId";
                lookupVessel.Properties.DataSource = source;
                lookupVessel.EditValue = lookupVessel.Properties.GetDataSourceValue("nVesselId", 0);
            }
            else
            {
                var query2 = from v in dsShortPlan.tblVessel.AsQueryable()
                             select v;
                BindingSource source = new BindingSource { DataSource = query2 };
                lookupVessel.Properties.DisplayMember = "cVesselName";
                lookupVessel.Properties.ValueMember = "nVesselId";
                lookupVessel.Properties.DataSource = source;
                lookupVessel.EditValue = lookupVessel.Properties.GetDataSourceValue("nVesselId", 0);
            }


        }
        private void cmdChon_Click(object sender, EventArgs e)
        {
            if (dateThang.EditValue != null)
            {
                int haveVessel = dsShortPlan.tblShortPlan.AsEnumerable().Where(c => c.VesselID == Convert.ToInt32(lookupVessel.EditValue)).Count();
                if (haveVessel == 0)
                {
                    using (Database.dsShortPlanTableAdapters.tadCurrentOnboard tadCurrent = new Database.dsShortPlanTableAdapters.tadCurrentOnboard())
                    {
                        if (lookupVessel.EditValue == null)
                            tadCurrent.Fill(dsShortPlan.tblCurrentOnboard);
                        else
                            tadCurrent.FillByVessel(dsShortPlan.tblCurrentOnboard, Convert.ToInt32(lookupVessel.EditValue));
                    }
                    ////clear
                    //if (XtraMessageBox.Show("Bạn muốn thêm và xóa danh sách hiện tại?", "Cảnh báo", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    //{
                    //    //  if (XtraMessageBox.Show("Dữ liệu hiện tại sẽ bị xóa! Bạn chắc chắn muốn thêm", "Cảnh báo", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    //    dsShortPlan.tblShortPlan.Clear();
                    //}
                    for (int i = 0; i < dsShortPlan.tblCurrentOnboard.Rows.Count; i++)
                    {
                        DataRow row = dsShortPlan.tblShortPlan.NewRow();
                        row["PlanID"] = PlanID;
                        row["isDone"] = false;
                        row["VesselID"] = dsShortPlan.tblCurrentOnboard.Rows[i]["nVesselId"];
                        row["nCrewID"] = dsShortPlan.tblCurrentOnboard.Rows[i]["nCrewid"];
                        row["nContract"] = dsShortPlan.tblCurrentOnboard.Rows[i]["nContract"];
                        row["dEmbarkedDate"] = dsShortPlan.tblCurrentOnboard.Rows[i]["dEmbarked"];
                        //row["NgayDuKienThayTV"] = dateDuKienThay.EditValue;
                        row["DurationOnboard"] = Math.Round((double)((DateTime)dateThang.EditValue).Subtract((DateTime)dsShortPlan.tblCurrentOnboard.Rows[i]["dEmbarked"]).Days / 30, 1);
                        dsShortPlan.tblShortPlan.Rows.Add(row);
                    }
                }
                else { XtraMessageBox.Show(rm.GetString("sPlanConflict", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo)); }
            }
        }

        private void repositoryItemSearchLookUpEdit1_QueryPopUp(object sender, CancelEventArgs e)
        {


        }
        private void gridNganHan_Click(object sender, EventArgs e)
        {

        }
        private void viewNganHan_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            //Kiem tra neu nhap ngay len tau: hopdong,ngaydukien,crewnextline, phai nhập
            if (viewNganHan.GetFocusedRowCellDisplayText(colNgayLenTau) != "")
            {
                if (viewNganHan.GetFocusedRowCellDisplayText(colTGHopDong) == "")
                {
                    viewNganHan.SetColumnError(colTGHopDong, "Not Blank");
                    e.Valid = false;
                }
                if (viewNganHan.GetFocusedRowCellDisplayText(colnCrewIDNext) == "")
                {
                    viewNganHan.SetColumnError(colnCrewIDNext, "Not Blank");
                    e.Valid = false;
                }
            }
            //Kiem tra neu nhap ngay roi tau: 
            if (viewNganHan.GetFocusedRowCellDisplayText(colXuongTau) != "")
            {
                if (viewNganHan.GetFocusedRowCellDisplayText(colnCrewID) == "")
                {
                    viewNganHan.SetColumnError(colXuongTau, "Not Valid");
                    e.Valid = false;
                }
            }
            if (viewNganHan.GetFocusedRowCellValue(colTGDuKienLenTau) != DBNull.Value && viewNganHan.GetFocusedRowCellValue(colTGHopDong) != DBNull.Value)
            {
                DateTime dukienRoitau = ((DateTime)viewNganHan.GetFocusedRowCellValue(colTGDuKienLenTau)).AddMonths(Convert.ToInt32(viewNganHan.GetFocusedRowCellValue(colTGHopDong)));
                viewNganHan.SetFocusedRowCellValue(colTGDuKienRoiTau, dukienRoitau);
            }


        }
        private void savePlan()
        {
            tblKeHoachBindingSource.EndEdit();
            tblKeHoachTableAdapter.Update(dsShortPlan.tblKeHoach);

            tblShortPlanBindingSource.EndEdit();
            tblShortPlanTableAdapter.Update(dsShortPlan.tblShortPlan);
            tblKeHoachTableAdapter.Done(isDone, PlanID);
            alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
            isNew = false;


        }
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                savePlan();
                _refreshData();
                Close();
                //thong bao cho parent form biet de cap nhat du lieu
            }
        }

        private void searchLookUpEdit2_EditValueChanged(object sender, EventArgs e)
        {
            if (lookupVessel.EditValue != null)
                tblShortPlanBindingSource.Filter = "VesselID=" + lookupVessel.EditValue;
        }


        private void searchLookUpEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.IsLeft)
            {
                tblShortPlanBindingSource.Filter = "";
                lookupVessel.EditValue = null;
            }
        }

        private void splitContainerControl1_Panel1_Leave(object sender, EventArgs e)
        {
            splitContainerControl1.Collapsed = true;
        }

        private void viewNganHan_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column == crewSTT && e.RowHandle > -1)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private int ToOnBoard(UserControl.ucAssignment u, MyArgs e)
        {
            return u.updateOnBoard(e);
        }
        private int ToOnVation(UserControl.ucAssignment u, MyArgs e)
        {
            return u.updateOnVacation(e);
        }

        private bool checkValidate(int rowhandle)
        {

            if ((viewNganHan.GetRowCellValue(rowhandle, colLenTau) == DBNull.Value &&
                viewNganHan.GetRowCellValue(rowhandle, colTGHopDong) == DBNull.Value &&
                viewNganHan.GetRowCellValue(rowhandle, colXuongTau) == DBNull.Value &&
                viewNganHan.GetRowCellValue(rowhandle, colnCrewIDNext) == DBNull.Value &&
                viewNganHan.GetRowCellValue(rowhandle, colNgayLenTau) == DBNull.Value) || (bool)viewNganHan.GetRowCellValue(rowhandle, colDOne) == true)
            {
                return false;
            }


            return true;
        }

        private void cmdSelect_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (dxValidationProvider1.Validate())
                if (viewNganHan.FocusedRowHandle > -1)
                {
                    frmSugestListShortPlan f = new frmSugestListShortPlan();
                    //truyen tham so 
                    Database.dsShortPlanTableAdapters.tblRankTableAdapter tadRank = new Database.dsShortPlanTableAdapters.tblRankTableAdapter();
                    int vesselID = (int)viewNganHan.GetFocusedRowCellValue(colVesselID);
                    string RankName = "";
                    int RankLower1 = 0;
                    int RankLower2 = 0;
                    int RankType = 0;
                    int _rankID = 0;
                    DateTime ngaydukien = (DateTime)dateDuKienThay.EditValue;


                    if (viewNganHan.GetFocusedRowCellDisplayText(colRank) != "")
                    {
                        RankName = viewNganHan.GetFocusedRowCellDisplayText(colRank);
                        RankType = Convert.ToInt32(tadRank.returnRankTypeID_RankLevel(viewNganHan.GetFocusedRowCellDisplayText(colRank)).Rows[0]["iRankType"]);
                        int RankLevel = Convert.ToInt32(tadRank.returnRankTypeID_RankLevel(viewNganHan.GetFocusedRowCellDisplayText(colRank)).Rows[0]["nRankLevel"]);
                        int maxLevel = Convert.ToInt32(tadRank.returnMaxLevel(RankType));
                        _rankID = Convert.ToInt32(viewNganHan.GetFocusedRowCellDisplayText(rankID));
                        if (RankLevel <= maxLevel - 1)
                        {
                            if (tadRank.returnLowerRank(RankType, RankLevel + 1) != null)
                                RankLower1 = (int)tadRank.returnLowerRank(RankType, RankLevel + 1);
                        }
                        string[] rankList = new string[] { "CAPT", "C/O", "2/O", "3/O", "C/E", "1/E", "2/E", "3/E" };
                        if (rankList.Contains(RankName.ToUpper()))
                        {
                            if (RankLevel <= maxLevel - 2)
                            {
                                if (tadRank.returnLowerRank(RankType, RankLevel + 2) != null)
                                    RankLower2 = (int)tadRank.returnLowerRank(RankType, RankLevel + 2);
                            }
                        }
                    }
                    f.ngaydukien = ngaydukien;
                    f.RankTypeID = RankType;
                    f.RankName = RankName;
                    f.RankLower1 = RankLower1;
                    f.RankLower2 = RankLower2;
                    f.VesselID = vesselID;
                    f.RankID = _rankID;
                    //Hien thi
                    f.ShowDialog();

                    viewNganHan.SetFocusedRowCellValue(colnCrewIDNext, f.crewID);
                    if (f.ngayLenXuong != new DateTime(1900, 1, 1))
                        viewNganHan.SetFocusedRowCellValue(colNgaylenRoiTau, f.ngayLenXuong);
                }
        }
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            dsShortPlan.tblShortPlan.Clear();
        }

        private void dxValidationProvider1_ValidationFailed(object sender, DevExpress.XtraEditors.DXErrorProvider.ValidationFailedEventArgs e)
        {
            splitContainerControl1.Collapsed = false;
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                tblKeHoachBindingSource.RemoveCurrent();
                tblKeHoachBindingSource.EndEdit();
                tblKeHoachTableAdapter.Update(dsShortPlan.tblKeHoach);
                _refreshData();
                this.Close();
            }
        }

        private void viewNganHan_ShowingEditor(object sender, CancelEventArgs e)
        {
            if ((bool)viewNganHan.GetFocusedRowCellValue(colDOne) == true)
            {
                e.Cancel = true;

            }
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
        int selectedVessel = 0;
        private void cmdAdd_Click(object sender, EventArgs e)
        {

            if (lookupVessel.EditValue == null)
            {
                XtraMessageBox.Show(rm.GetString("sVesselSelect", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo));
                lookupVessel.ShowPopup();
            }
            else
            {

                selectedVessel = Convert.ToInt32(lookupVessel.EditValue);

                tblShortPlanBindingSource.AddNew();
                tblShortPlanBindingSource.MoveLast();
                tblShortPlanBindingSource.EndEdit();

            }

        }

        private void viewNganHan_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            // khoi tao row 
            if (selectedVessel != 0)
            {
                //vessel
                viewNganHan.SetRowCellValue(e.RowHandle, colVesselID, selectedVessel);
                //is done
                viewNganHan.SetRowCellValue(e.RowHandle, colIsDone, false);
                //planID
                viewNganHan.SetRowCellValue(e.RowHandle, colPlanID, PlanID);
            }
        }
        private void cmdDone_Click(object sender, EventArgs e)
        {
            //if (!isNew)
            //{
            //save

            MyArgs ex = new MyArgs();
            UserControl.ucAssignment u = new UserControl.ucAssignment();
            if (XtraMessageBox.Show(rm.GetString("sPlanConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {

                //dem so thuyen vien hoan thanh
                int count = 0;
                for (int i = 0; i < dsShortPlan.tblShortPlan.Rows.Count; i++)
                {
                    if (checkValidate(i))
                    {

                        //neu nhap day du thong tin len va xuong
                        if (viewNganHan.GetRowCellValue(i, colNgayLenTau) != DBNull.Value && viewNganHan.GetRowCellValue(i, colXuongTau) != DBNull.Value && viewNganHan.GetRowCellValue(i, colnCrewID) != DBNull.Value)
                        {
                            ex.ngayLenTau = (DateTime)viewNganHan.GetRowCellValue(i, colNgayLenTau);
                            ex.hopdong = Convert.ToInt32(viewNganHan.GetRowCellValue(i, colTGHopDong));
                            ex.vesselID = Convert.ToInt32(viewNganHan.GetRowCellValue(i, colVesselID));
                            ex.ngayxuong = (DateTime)viewNganHan.GetRowCellValue(i, colXuongTau);
                            ex.crewID = viewNganHan.GetRowCellValue(i, colnCrewID).ToString();
                            ex.crewIDNextLine = viewNganHan.GetRowCellValue(i, colnCrewIDNext).ToString();
                            ex.NoiRoi = viewNganHan.GetRowCellValue(i, "NoiXuong").ToString();
                            ex.NoiLen = viewNganHan.GetRowCellValue(i, "NoiLen").ToString();
                            ex.rankID = Convert.ToInt32(((DataRowView)rpLookupCrew.GetDataSourceRowByDisplayValue(viewNganHan.GetRowCellDisplayText(i, colRank))).Row["nRankID"]); ;
                            UpdaterRank(i);
                            if (ToOnBoard(u, ex) > 0)
                                ToOnVation(u, ex);
                            viewNganHan.SetRowCellValue(i, colDOne, true);
                            count++;
                        }
                        //truong hop ve khong thay
                        if (viewNganHan.GetRowCellValue(i, colXuongTau) != DBNull.Value && viewNganHan.GetRowCellValue(i, colnCrewID) != DBNull.Value && viewNganHan.GetRowCellValue(i, colNgayLenTau) == DBNull.Value)
                        {
                            ex.ngayxuong = (DateTime)viewNganHan.GetRowCellValue(i, colXuongTau);
                            ex.NoiRoi = viewNganHan.GetRowCellValue(i, "NoiXuong").ToString();
                            ex.crewID = viewNganHan.GetRowCellValue(i, colnCrewID).ToString();
                            ex.rankID = Convert.ToInt32(((DataRowView)rpLookupCrew.GetDataSourceRowByDisplayValue(viewNganHan.GetRowCellDisplayText(i, colRank))).Row["nRankID"]);
                            UpdaterRank(i);
                            ToOnVation(u, ex);
                            viewNganHan.SetRowCellValue(i, colDOne, true);
                            count++;

                        }
                        //truong hop bo xung
                        if (viewNganHan.GetRowCellDisplayText(i, colnCrewID) == "" && viewNganHan.GetRowCellDisplayText(i, colNgayLenTau) != "")
                        {
                            ex.crewIDNextLine = viewNganHan.GetRowCellValue(i, colnCrewIDNext).ToString();
                            ex.ngayLenTau = (DateTime)viewNganHan.GetRowCellValue(i, colNgayLenTau);
                            ex.crewIDNextLine = viewNganHan.GetRowCellValue(i, colnCrewIDNext).ToString();
                            ex.vesselID = Convert.ToInt32(viewNganHan.GetRowCellValue(i, colVesselID));
                            ex.NoiLen = viewNganHan.GetRowCellValue(i, "NoiLen").ToString();
                            ex.hopdong = Convert.ToInt32(viewNganHan.GetRowCellValue(i, colTGHopDong));
                            ex.rankID = Convert.ToInt32(((DataRowView)rpLookupCrew.GetDataSourceRowByDisplayValue(viewNganHan.GetRowCellDisplayText(i, colChucDanh))).Row["nRankID"]); ;
                            UpdaterRank(i);
                            ToOnBoard(u, ex);
                            viewNganHan.SetRowCellValue(i, colDOne, true);
                            count++;
                        }
                    }
                }
                //neu da hoan thanh het
                tblKeHoachTableAdapter.Done(true, PlanID);
                //int isDOneCount = dsShortPlan.tblShortPlan.AsEnumerable().Where(c => c.isDone == true).Count();
                
                //if (dsShortPlan.tblShortPlan.Rows.Count == isDOneCount)
                //    isDone = true;
                if (Program.language == "en")
                {
                    XtraMessageBox.Show(String.Format("{0} records \r\n has updated successfull", count));
                }
                else
                {
                    XtraMessageBox.Show(String.Format("{0} bản ghi \r\n cập nhật thành công", count));
                }

                savePlan();
                _refreshData();

            }
            //}
            //else
            //{
            //    if (Program.language == "en")
            //    {
            //        XtraMessageBox.Show("You must save plan", "Information");
            //    }
            //    else
            //    {
            //        XtraMessageBox.Show("Xin vui lòng lưu kế hoạch trước!", "Thông báo");
            //    }

            //}
        }
        private void UpdaterRank(int i)
        {
            if (viewNganHan.GetRowCellDisplayText(i, colRank) != "" && viewNganHan.GetRowCellDisplayText(i, colChucDanh) != "" && viewNganHan.GetRowCellDisplayText(i, colRank) != viewNganHan.GetRowCellDisplayText(i, colChucDanh))
            {
                Database.dsShortPlanTableAdapters.tadUpdateRank tadRank = new Database.dsShortPlanTableAdapters.tadUpdateRank();
                int rankID = Convert.ToInt32(((DataRowView)rpLookupCrew.GetDataSourceRowByDisplayValue(viewNganHan.GetRowCellDisplayText(i, colRank))).Row["nRankID"]);
                tadRank.UpdateRank(rankID, viewNganHan.GetRowCellValue(i, colnCrewIDNext).ToString());
            }
        }
        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {


        }

        private void dateDuKienThay_EditValueChanged(object sender, EventArgs e)
        {

            //for (int i = 0; i < viewNganHan.RowCount; i++)
            //{
            //    viewNganHan.SetRowCellValue(i, "NgayDuKienThayTV", dateDuKienThay.EditValue);
            //}
        }

        private void barButtonItem1_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {


            savePlan();
            try
            {
                Reports.rptToTrinh totring = new Reports.rptToTrinh();
                totring.loadData(PlanID, (Int32)lookupVessel.EditValue);
                totring.ShowPreview();
            }
            catch
            { }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            savePlan();

            try
            {
                Reports.rptDanhSachDuKienDieuDong r = new Reports.rptDanhSachDuKienDieuDong();
                r.loadData(PlanID, (Int32)lookupVessel.EditValue);
                r.ShowPreview();
            }
            catch
            { }


        }

        private void btnbanCamKet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmBanCamKet f = new frmBanCamKet();
            f.planid = PlanID;
            f.Show();
        }

        private void barButtonItem4_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog f = new SaveFileDialog();
            f.Filter = "Excel Files | *.xls";
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                viewNganHan.ExportToXls(f.FileName);
            }
        }

        private void barButtonItem5_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                List<string> listNext = (from c in dsShortPlan.tblShortPlan.AsEnumerable()
                                         where !(c.IsNgayDuKienThayTVNull() || c.IsnCrewIDNextNull()) && c.isDone == false
                                         select c.nCrewIDNext).ToList<string>();

                List<string> listOff = (from c in dsShortPlan.tblShortPlan.AsEnumerable()
                                        where !(c.IsNgayDuKienThayTVNull() || c.IsnCrewIDNull()) && c.isDone == false
                                        select c.nCrewID).ToList<string>();

                Reports.rptThayThuyenVien r = new Reports.rptThayThuyenVien();
                r.loadData(PlanID, (Int32)lookupVessel.EditValue, listNext, listOff);
                r.ShowPreview();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Đã có lỗi sau xảy ra: " + ex.ToString());
            }

        }
    }
}